package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.AdSensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.wemedia.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Autowired
    private CacheService cacheService;

    /**
     * 新增敏感词
     *
     * @param adSensitiveDto
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitiveDto adSensitiveDto) {
        //1、参数校验
        if (adSensitiveDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、转化为实体类
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(adSensitiveDto, wmSensitive);
        wmSensitive.setCreatedTime(new Date());

        //3、已有的不保存
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, adSensitiveDto.getSensitives()));

        if (wmSensitives != null && !wmSensitives.isEmpty()) {
            for (WmSensitive sensitive : wmSensitives) {
                if (sensitive.getSensitives().equals(adSensitiveDto.getSensitives())) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
                }
            }
        }

        //4、添加进数据库
        wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询敏感词
     *
     * @param adSensitiveDto
     * @return
     */
    @Override
    public ResponseResult listSensitive(AdSensitiveDto adSensitiveDto) {
        //1、参数校验
        if (adSensitiveDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检验页码
        adSensitiveDto.checkParam();

        //2、设置分页查询参数
        Page page = new Page(adSensitiveDto.getPage(), adSensitiveDto.getSize());

        //3、设置分页查询条件
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();

        //3.1按敏感词名称模糊查询
        if (adSensitiveDto.getSensitives() != null) {
            wrapper.like(WmSensitive::getSensitives, adSensitiveDto.getSensitives());
        }
        //3.2按创建时间倒序查询
        wrapper.orderByDesc(WmSensitive::getCreatedTime);

        //4、调用分页查询方法
        page(page, wrapper);

        //5、封装进PageResponseResult,返回
        PageResponseResult result = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ResponseResult delSensitive(Integer id) {


        //1、删除redis敏感词
        //1.1、根据id查询数据库
        WmSensitive wmSensitive = wmSensitiveMapper.selectById(id);

        //1.2先获取redis数据库敏感词信息
        String sensitivesJSON = cacheService.get("sensitives");
        if (sensitivesJSON != null && !sensitivesJSON.isEmpty()) {
            //1.3解析redis数据库敏感词信息
            List<String> stringList = JSON.parseArray(sensitivesJSON, String.class);
            //1.4删除redis列表对应的敏感词信息
            stringList.remove(wmSensitive.getSensitives());
            //1.5将更新后的列表写回redis
            cacheService.set("sensitives", JSON.toJSONString(stringList));
        }

        //2、删除数据库敏感词
        wmSensitiveMapper.deleteById(id);
        //3、返回操作结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param adSensitiveDto
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitiveDto adSensitiveDto) {
        //1、参数校验
        if (adSensitiveDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、转化为实体类
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(adSensitiveDto, wmSensitive);
        wmSensitive.setCreatedTime(new Date());

        //3、修改参数
        wmSensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
